<?php
namespace app\news\model;
use app\rbac\model\AdminM;
use think\Validate;
use think\Db;
use app\base\model\Base;
class ArticleM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'title' => 'require',
               'cat_id' => 'require|number',
               'content' => 'require',
               'intro' => 'require',
               'sort_num' => 'require|number',
           ];
        $msg = [];

        $filed = [
            'title'=>'文章标题',
            'cat_id'=>'文章分类',
            'content'=>'文章内容',
            'intro'=>'文章介绍',
            'sort_num'=>'排序',
        ];
           $validate = new Validate($rule,$msg,$filed);
           $validate->scene('add', ['title','cat_id','content','intro','sort_num']);
           $validate->scene('edit', ['title','cat_id','content','intro','sort_num']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        //验证数据
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }

        $insertData = [];
        //处理图片
         $ret = uploadOne('logo', array(
         array(800, 800),
         array(600, 600),
         array(300, 300),
         array(100, 100),
         ));
         if($ret['ok'] == 1)
         {
             $insertData['logo'] = toQiNiu($ret['images'][0]);   // 原图地址
             $insertData['mbig_logo'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
             $insertData['big_logo'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
             $insertData['mid_logo'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
             $insertData['sm_logo'] = toQiNiu($ret['images'][4]);   // 第三个缩略图地址
            }
         else if($ret['ok'] == 2)
         {
         }
         else
         {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
         }

         //时间
        $time = time();

        $insertData['title'] = $data['title'];
        $insertData['cat_id'] = $data['cat_id'];
        $insertData['admin_id'] = session('admin_id');
        $insertData['content'] = $data['content'];
        $insertData['intro'] = $data['intro'];
        $insertData['keywords'] = $data['keywords'];
        $insertData['sort_num'] = $data['sort_num'];
        $insertData['updated_at'] = $time;
        $insertData['created_at'] = $time;


        $id = Db::table('news_article')->insertGetId($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '新增失败';
            return false;
        }
        return true;
    }

    /**
     * 全部数据
     */
    public function lst($page = 15)
    {
        $admin_id = session('admin_id');
        $adminModel = new AdminM();
        $res = $adminModel->isAdmin($admin_id);
        $where = [];
        if($res === false)
        {
            $where['a.admin_id'] = $admin_id;
        }
        //显示未删除的
        $where['a.is_usable'] = 1;
        $data = Db::table('news_article')
            ->alias('a')
            ->join('news_article_cat ac','ac.id = a.cat_id','LEFT')
            ->where($where)
            ->field('a.id,a.title,a.keywords,a.sm_logo,a.created_at,a.updated_at,a.publish_at,a.is_show,a.is_sh,ac.name as cat_name,a.review_at')
            ->paginate($page);
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //处理图片
        $ret = uploadOne('logo', array(
            array(800, 800),
            array(600, 600),
            array(300, 300),
            array(100, 100),
        ));
        if($ret['ok'] == 1)
        {
            $insertData['logo'] = toQiNiu($ret['images'][0]);   // 原图地址
            $insertData['mbig_logo'] = toQiNiu($ret['images'][1]);   // 第一个缩略图地址
            $insertData['big_logo'] = toQiNiu($ret['images'][2]);   // 第二个缩略图地址
            $insertData['mid_logo'] = toQiNiu($ret['images'][3]);   // 第三个缩略图地址
            $insertData['sm_logo'] = toQiNiu($ret['images'][4]);   // 第三个缩略图地址
        }
        else if($ret['ok'] == 2)
        {
        }
        else
        {
            $this->code = -1;
            $this->msg = $ret['error'];
            return false;
        }

        //时间
        $time = time();

        $insertData['title'] = $data['title'];
        $insertData['cat_id'] = $data['cat_id'];
        $insertData['content'] = $data['content'];
        $insertData['intro'] = $data['intro'];
        $insertData['keywords'] = $data['keywords'];
        $insertData['sort_num'] = $data['sort_num'];
        $insertData['updated_at'] = $time;


        $id = Db::table('news_article')->where('id',$data['id'])->update($insertData);
        if(!$id)
        {
            $this->code = -1;
            $this->msg = '修改失败';
            return false;
        }
        return true;
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        return Db::table('news_article')->where('id',$id)->update(['is_usable'=>0]);
    }
    /**
     * 发布
     * @param $id
     */
    public function publish($id)
    {
        $res = Db::table('news_article')->where('id',$id)->field('is_show')->find();
        $update = [];
        if($res['is_show'] == 1)
        {
            $update['is_show'] = 0;
            $update['is_sh'] = 0;//重新审核
        }
        else
        {
            $update['is_show'] = 1;
            $update['publish_at'] = time();

        }
        return Db::table('news_article')->where('id',$id)->update($update);
    }


    /**
     * 审核
     * @param $id
     */
    public function review($id)
    {
        return Db::table('news_article')->where('id',$id)->update(['is_sh'=>1,'review_at'=>time()]);
    }

}
